package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

public class _6_ZigzagConversion {
    public static void main(String[] args) {
        Solution solution = new _6_ZigzagConversion().new Solution();
        String s = solution.convert("AB", 1);
        System.out.println(s);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) {
                return s;
            }

            List<StringBuilder> list = new ArrayList<>(numRows);
            for (int i = 0; i < numRows; i++) {
                list.add(new StringBuilder());
            }

            int idx = 0;
            boolean flag = true;

            for (int i = 0; i < s.length(); i++) {
                list.get(idx).append(s.charAt(i));
                idx = flag ? idx + 1 : idx - 1;
                if (idx == numRows - 1 || idx == 0) {
                    flag = !flag;
                }
            }
            StringBuilder sb = new StringBuilder();
            for (StringBuilder stringBuilder : list) {
                sb.append(stringBuilder);
            }
            return sb.toString();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}